/*
 * Powered By zindesign
 * Web Site: http://www.zindesign.cn
 * Since 2012 - 2012
 */

package cn.zindesign.platform.action;

import java.util.*;

import cn.zindesign.platform.model.*;
import cn.zindesign.platform.dao.*;
import cn.zindesign.platform.service.*;

/**
 * @author lihui email:157370862@qq.com
 * @version 1.0
 * @since 1.0
 */


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import cn.zindesign.base.BaseAction;
import cn.zindesign.platform.model.User;
import cn.zindesign.system.Page;
import cn.zindesign.system.PageList;
import cn.zindesign.utils.H;

public class UserAction extends BaseAction implements ServletRequestAware {

	private HttpServletRequest request;
	
	private User user;
	
	private String p;
	
	private String t;
	
	private String keyword;
	
	private String url;
	

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = H.getParamValue(url);
	}

	/**
	 * @return the keyword
	 */
	public String getKeyword() {
		return keyword;
	}

	/**
	 * @param keword the keword to set
	 */
	public void setKeyword(String keyword) {
		this.keyword = H.getParamValue(keyword);
	}

	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	/**
	 * 初始化新增界面数据
	 * @return
	 */
	public String toAdd(){
		
		return "toAdd";
	}
	
	/**
	 * 初始更新界面数据
	 * @return
	 */
	public String toUpdate(){
		user = (User) getUserService().selectById("UserMapper", user.getId());
		if(user!=null){
			initParams();
			request.setAttribute("p", p);
			request.setAttribute("t", t);
			request.setAttribute("keyword", keyword);
			request.setAttribute("user", user);
			return "toUpdate";
		}else{
			return "error";
		}
	}
	
	/**
	 * 新增
	 * @return
	 */
	public String save(){
		int i = getUserService().doInsert("UserMapper", user);
		if(i>0){
			request.setAttribute("url", "/platform/userAction!queryList");
			return "success";
		}else{
			return "error";
		}
	}
	
	/**
	 * 更新
	 * @return
	 */
	public String update(){
		int i = getUserService().doUpdateById("UserMapper", user);
		if(i>0){
			initParams();
			request.setAttribute("url", url);
			return "success";
		}else{
			return "error";
		}
		
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		int i = getUserService().doDeleteById("UserMapper", user.getId());
		if(i>0){
			initParams();
			request.setAttribute("url", "/platform/userAction!queryList?p="+p+"&keyword="+keyword);
			return "success";
		}else{
			return "error";
		}
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	public String batchDelete(){
		String ids[] = H.getParamValues(request, "ids");
		int i = getUserService().doBatchDelete("UserMapper", ids);
		if(i>0){
			initParams();
			request.setAttribute("url", "/platform/userAction!queryList?p="+p+"&keyword="+keyword);
			return "success";
		}else{
			return "error";
		}
	}
	
	/**
	 * 显示详细
	 * @return
	 */
	public String show(){
		user = (User) getUserService().selectById("UserMapper", user.getId());
		if(user!=null){
			return "show";
		}else{
			return "error";
		}
		
	}
	
	
	/**
	 * 显示管理列表
	 * @return
	 */
	public String queryList(){
		getManageList();
		return "queryList";
	}
	
	/**
	 * 初始化参数
	 */
	void initParams(){
		p = H.getParamValue(request, "p");
		t = H.getParamValue(request, "t");
	}
	
	/**
	 * 获取管理列表数据
	 */
	void getManageList(){
		initParams();
		try{
			Map<String,Object> map = new HashMap<String,Object>();
			Page page = new Page(request);
			page.setPerPageNum(10);
			if(p!=null&&!p.equals("")){
				page.setPage(Integer.parseInt(p));
			}else{
				page.setPage(1);
			}
			if(t!=null&&!t.equals("")){
				page.setTotals(Long.parseLong(t));
			}
			page.doPageBreak();
			//page分页使用
			map.put("page", page);
			//这里添加其他查询条件
			map.put("keyword", keyword);
			PageList<User> pl = (PageList)getUserService().selectPageListByProperty("UserMapper", map);
			request.setAttribute("list",pl.getList());
			request.setAttribute("page", pl.getP());
			request.setAttribute("keyword",keyword);
			}catch(Exception e){
				e.printStackTrace();
			}
	}
}
